﻿using System;
using System.Linq;
using System.Web.Mvc;
using Xenta.Utils;
using Xenta.Attributes;
using Xenta.Web.Controllers;

namespace Xenta.Web.Areas.Commerce.Controllers
{
    [Authenticate, InRole("ADMIN")]
    public sealed class VolumeDiscountRuleController : ExtController
    {
        #region GET/POST Actions

        [HttpPost, PersistState]
        public ActionResult Create(string name,
            string[] commonFlags,
            string thresholdLower,
            string thresholdUpper,
            string amount,
            string[] flags,
            string[] flagsG1,
            string[] flagsG2,
            string validFrom,
            string validTo)
        {
            try
            {
                var m = Svc.ProcessOperation("CreateVolumeDiscountRule", new
                {
                    Name = name,
                    CommonFlags = commonFlags.Merge(","),
                    ThresholdLower = Svc.Numeric(thresholdLower),
                    ThresholdUpper = Svc.Numeric(thresholdUpper),
                    Amount = Svc.Numeric(amount),
                    Flags = flags.Union(flagsG1).Union(flagsG2).Merge(","),
                    ValidFrom = Svc.DateTime(validFrom),
                    ValidTo = Svc.DateTime(validTo)
                });
                return RedirectToAction(Loc.Act("Edit").Ctrl("SalesRule").Set(new
                {
                    id = JDM.Int32(m.EntityID)
                }));
            }
            catch(Exception ex)
            {
                ModelState.AddModelError("API", ex);
            }
            return RedirectToAction(Loc.Act("Create").Ctrl("SalesRule").Set(new
            {
                code = "VOLUME_DISCOUNT"
            }));
        }

        [HttpPost, PersistState]
        public ActionResult Update(int id, 
            string name,
            string[] commonFlags,
            string thresholdLower,
            string thresholdUpper,
            string amount,
            string[] flags,
            string[] flagsG1,
            string[] flagsG2,
            string validFrom,
            string validTo)
        {
            try
            {
                Svc.ProcessOperation("UpdateVolumeDiscountRule", new
                {
                    EntityID = id,
                    Name = name,
                    CommonFlags = commonFlags.Merge(","),
                    ThresholdLower = Svc.Numeric(thresholdLower),
                    ThresholdUpper = Svc.Numeric(thresholdUpper),
                    Amount = Svc.Numeric(amount),
                    Flags = flags.Union(flagsG1).Union(flagsG2).Merge(","),
                    ValidFrom = Svc.DateTime(validFrom),
                    ValidTo = Svc.DateTime(validTo)
                });
            }
            catch(Exception ex)
            {
                ModelState.AddModelError("API", ex);
            }
            return RedirectToAction(Loc.Act("Edit").Ctrl("SalesRule").Set(new
            {
                id
            }));
        }

        #endregion
    }
}